<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="loginRules"
          ref="loginFormRef"
        >
          <h1>Hello</h1>
          <h2>通用管理系统模板</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              type="primary"
              class="login_btn"
              @click="checkuser"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
// 引入用户相关的小仓库
import { useRouter } from "vue-router";
import { ElNotification, FormInstance } from "element-plus";
import { loginForm } from "@/api/user/type";
import { userStore } from "@/store/module/acl/user";
import { getTime } from "../../utils/dataSeg";
// 表单验证功能编写
import { loginRules } from "./loginValidate";

//pinia封装的关于用户的所有操作，为了保存成功以后的token
let userApp = userStore();

// 获取路由器 ， 成功以后的跳转功能
let $router = useRouter();
// 定义变量控制按钮加载效果
let loading = ref(false);

// 收集账号和密码的数据
let loginForm = reactive<loginForm>({
  username: "admin",
  password: "admin",
});

const loginFormRef = ref<FormInstance>();

//登陆方法处理
const checkuser = async () => {
  //用来处理加载功能
  loading.value = true;
  // 调用封装
  console.log(loginFormRef.value?.validate());

  loginFormRef.value
    ?.validate()
    .then(() => {
      userApp
        .userLogin(loginForm)
        .then((res: any) => {
          //成功以后的处理

          if (res == "success") {
            // 处理上次请求信息保留问题
            let param: any = $router.currentRoute.value.query.redirect;
            $router.push({ path: param || "/" });
            ElNotification({
              type: "success",
              message: "欢迎回来",
              title: `${getTime()}好,${userApp.username}先生`,
            });
          }
          loading.value = false;
          //出出错误以后的处理
        })
        .catch((e) => {
          //失败去掉加载功能
          loading.value = false;
          ElNotification({
            type: "error",
            message: e.message,
            title: "登陆失败",
          });
        });
    })
    .catch(() => {
      loading.value = false;
      ElNotification({
        type: "error",
        message: "请修正输入的用户名或者密码为正确规则项",
        title: "登陆失败",
      });
    });
};
</script>
<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
